﻿using System.IO.Compression;
using System.Web;
using Castle.MonoRail.Framework;

namespace eduSrc.Web
{
    public class CompressFilter : IFilter
    {
        #region IFilter 成员

        bool IFilter.Perform(ExecuteEnum exec, IRailsEngineContext context, Controller controller)
        {
            string acceptEncoding = context.Request.Headers["Accept-Encoding"];
            if (string.IsNullOrEmpty(acceptEncoding))
            {
                return false;
            }

            acceptEncoding = acceptEncoding.ToUpperInvariant();

            IResponse response = context.Response;
            if (acceptEncoding.Contains("GZIP"))
            {
                response.AppendHeader("Content-encoding", "gzip");
                //response.BinaryWrite(new GZipStream(response.OutputStream, CompressionMode.Compress));
                return true;
            }
            else if (acceptEncoding.Contains("DEFLATE"))
            {
                response.AppendHeader("Content-encoding", "deflate");
                //response.BinaryWrite(new GZipStream(response.OutputStream, CompressionMode.Compress));
                return true; 
            }
            return false;
        }

        #endregion
    }
}